<?php
namespace src\logic;
use \classlib\core as C;
class levent {
	/**
	 * 奖励数量的设置 缓存Key信息
	 * @param string $sid
	 * @return string
	 */
	public static function event_gift_ckey($sid) {
		return 'st_gift_ttl_'.$sid;
	}
	
	/**
	 * 做直播间会员送礼的统计
	 * @param unknown $sid
	 */
	public static function event_stat_ckey($sid) {
		return 'st_stat_'.$sid;
	}
	
	/**
	 * 创建一个直播场次统计信息
	 * @param int   $userid
	 * @param array $studio
	 * @param array $user
	 * @return int  $evtid
	 */
	public static function create(&$studio, &$user) {
		$eorm   = C\model::load('pro.pl.event')->set_dev($studio['id']);
		//将之前的直播场次关闭
		$eorm->get_orm()->where('studioid', $studio['id'])->where('offtime', 0)
			->update(array('offtime'=>C\config::$nowtime));
		//创建新的直播场次
		$finance= C\model::load('pro.pl.finance')->set_dev($studio['userid'])->get_one($studio['userid']);
		$olmax  = limapi::getolbyid($studio['id']);
		$field  = array('studioid'=>$studio['id'], 'userid'=>$studio['userid'], 'like'=>$studio['charms'], 'earing'=>$finance['earningcoin'],
			'salary'=>$finance['salarycoin'], 'exp'=>$studio['exp'], 'xfans'=> $studio['xfans'], 'lv'=>$user['lv'], 'olmax'=>$olmax, 
			'aritime'=>C\config::$nowtime, 'lasttime'=>C\config::$nowtime, 'ip'=>C\get_ip(), 'agent'=>$_SERVER['HTTP_USER_AGENT'],
			'title'=>$studio['title'], 'stime'=>C\config::$nowtime);
		$evtid  = $eorm->new_one($field);
		//将这期间送礼信息归类到当前的直播场次
		C\model::load('pro.log.moneyflow')->get_orm()->where('anchorid', $studio['userid'])
			->where('reftype', 1)->where('eventid', 0)->update(array('eventid'=>$evtid));
		self::seteid($studio['id'], $evtid);	
		return $evtid;
	}
	
	/**
	 * 设置直播间当前的直播场次ID记录
	 * @param unknown $sid
	 * @param unknown $eid
	 */
	public static function seteid($sid, $eid) {
		$ckey = 'st_eid_'.$sid;	
		$rds  = \classlib\cache::inst('redis');
		$rds->set($ckey, $eid);
		//送礼的数量重置为 0
		$rds->set(self::event_gift_ckey($sid), 0);
		$rds->inst()->delete(self::event_stat_ckey($sid));
	}
	
	/**
	 * 获取直播间场次ID记录
	 * @param unknown $sid
	 * @return int $eid
	 */
	public static function geteid($sid, $userid) {
		$ckey = 'st_eid_'.$sid;
		$eid  = \classlib\cache::inst('redis')->get($ckey);
		if (!is_numeric($eid)) {
			$where  = array('studioid'=>$sid);
			$eid    = C\model::load('pro.pl.event')->set_dev($sid)
				->get_value('id', $where, 'id', \classlib\orm::DESC);
			\classlib\cache::inst('redis')->set($ckey, intval($eid));
		}
		return $eid;
	}
	
	/**
	 * 统计数累计上去，记录该直播场次的统计数排行版
	 * @param int $sid
	 * @param int $userid
	 * @param int $coin
	 * @param int $nums
	 * @param Redis $rds
	 */
	public static function evtstat($sid, $userid, $coin, $nums, $rds) {
		//统计数累计上去
		$data = array('nums'=>0, 'rank'=>array());
		if ($nums > 0) {
			$ckey   = self::event_gift_ckey($sid);
			$rds->incrBy($ckey, $nums);
			$data['nums'] = $rds->get($ckey);
		}
		if ($coin > 0) {
			$ckey   = self::event_stat_ckey($sid);
			if (!$rds->zIncrBy($ckey, $coin, $userid)) {
				$rds->zAdd($ckey, $coin, $userid);
			}
			$data['rank'] = self::evtrank($sid, 0, 10, $rds);
			limapi::notifystudio($sid, json_encode($data), 0, 20016);
		}
	}
	
	/**
	 * 获取当个用户直播场次贡献值
	 * @param int $sid
	 * @param int $userid
	 * @param Redis $rds
	 */
	public static function evtonestat($sid, $userid, $rds) {
		$ckey   = self::event_stat_ckey($sid);
		return (int)$rds->zScore($ckey, $userid);
	}
	
	
	
	/**
	 * 会员消费排行版资料数据
	 * @param unknown $sid
 	 * @param unknown $start
	 * @param unknown $limit
	 * @param Redis   $rds
	 * @return $list
	 */
	public static function evtrank($sid, $start, $limit, $rds) {
		$list    = array();
		$options = array('withscores'=>true, 'limit'=>array(0, $limit));
		$ckey    = self::event_stat_ckey($sid);
		$stats   = $rds->zRevRangeByScore($ckey, '+inf', '-inf', $options);
		if ($stats) {
			$uids= lvipsquad::uids($sid);
			$orm = C\model::load('pro.pl.userinfo');
			foreach ($stats as $userid=>$coin) {
				$user   = $orm->set_dev($userid)->get_one($userid);
				$isguard= ($uids && in_array($userid, $uids))? 1:0;
				$list[] = array('nickname'=>$user['nickname'], 'userid'=>$user['userid'], 'avtar'=>$user['thumb'], 
					'coin'=>$coin, 'lv'=>$user['lv'], 'isguard'=>$isguard, 'roler'=>$user['isanchor']);
			}
		}
		return $list;
	}
	
	/**
	 * 预约时间变动的时候发送消息通知
	 * @param array $studio
	 */
	public static function airNotify($studio, $airtime) {
		$sorm  = C\model::load('pro.log.subscibe');
		$where = array('studioid'=>$studio['id'], 'airtime'=>array(C\config::$nowtime, \classlib\orm::OP_GT));
		$list  = $sorm->get_column(0, -1, $where, 'userid');
		if ($list) {
			$lang   = C\lang::inst('web');
			$uorm   = C\model::load('pro.pl.userinfo');
			$user   = $uorm->set_dev($studio['userid'])->get_one($studio['userid']);
			$title  = $lang->txt('action', '80090', $user['nickname']);
			$oldtime= lshare::timezone($studio['airtime']);
			$newtime= lshare::timezone($airtime);
			$msg    = $lang->txt('action', '80091', $user['nickname'], $oldtime, $newtime);
			$sql    = 'INSERT INTO vs_msg (id,senderid,dictid,reftype,refid,title,msg,reveiverid,status,stime) VALUES';
			$stime  = C\config::$nowtime;
			foreach ($list as $userid) {
				$arr[] = "(NULL, 0, 1, 0, '{$userid}','{$title}','{$msg}',{$userid}, 1, {$stime})";
				//APP端PUSH通知
				api_sitemsg::onesignal($item['userid'], $title, $msg, 4, $studio['id']);
			}
			C\model::load('pro.pl.msg')->get_orm()->get_parser()->execute($sql.implode(',', $arr));
			C\model::load('pro.pl.msg')->set_dev();
		}
		$sorm->get_orm()->where('studioid', $sid)->update(array('airtime'=>$airtime, 'stime'=>C\config::$nowtime));
		$sorm->set_ver();
	}
}